package com.eugene.shiftscalendar;

import android.content.Context;

import java.util.Calendar;
import java.util.Date;

/**
 * Created by Eugene on 13.03.14.
 */
public class DateType {

    enum Type{
        Normal,
        Holiday,
        NormalThisMonth,
        HolidayThisMonth
    }

    boolean isHoliday(Calendar cal, Context context)
    {
        if(isUserChanged(cal, context)){
            return DateInfo.getDateInfo(cal, context) == 1;
        }

        if(Settings.showShifts){
            cal.set(Calendar.HOUR_OF_DAY, 1);
            cal.set(Calendar.MINUTE, 1);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            Date current = new Date(cal.getTimeInMillis());
            long days = (current.getTime() - Settings.shiftStart.getTime()) / (24 * 60 * 60 * 1000);
            int d = (int)days;
            if(d<0) d = -d+Settings.shiftWorkDay-1;
            d = d%(Settings.shiftRestDay + Settings.shiftWorkDay);
            return d >= Settings.shiftWorkDay;
        }
        else{
            if(Settings.showCountryHolidays2014 && isCountryHoliday(cal)==2) return true;
            int day = cal.get(Calendar.DAY_OF_WEEK);
            if(day == Calendar.SATURDAY || day == Calendar.SUNDAY){
                return true;
            }
            else return false;
        }
    }

    int isCountryHoliday(Calendar cal)
    {
        int year = cal.get(Calendar.YEAR);
        int month = cal.get(Calendar.MONTH); // 0 to 11
        int day = cal.get(Calendar.DAY_OF_MONTH); // 1 to 31 ?
        if(year == 2014){
            switch(month){
                case Calendar.JANUARY: if(day<=8) return 2; break;
                case Calendar.FEBRUARY: if(day==24) return 1; break;
                case Calendar.MARCH: if(day==7) return 1; if(day==10) return 2; break;
                case Calendar.APRIL: if(day==30) return 1; break;
                case Calendar.MAY: if(day<=2 || day==9) return 2; if(day==8) return 1; break;
                case Calendar.JUNE: if(day == 12 || day == 13) return 2; if(day==11) return 1; break;
                case Calendar.NOVEMBER: if(day==3 || day ==4) return 2; break;
                case Calendar.DECEMBER: if(day==31) return 1; break;
            }
        }

        return 0;
    }

    boolean isUserChanged(Calendar cal, Context context)
    {
        if(!Settings.useUserChanges) return false;
        int info = DateInfo.getDateInfo(cal, context);
        return info != -1;
    }
}
